from bson.timestamp import Timestamp
import pymysql
import pymssql
from mongosync import SqlSata, MongoSync, bson_ts_to_long
import datetime
import time


class Bl_SqlSata(SqlSata):
    def __init__(self):
        db = pymysql.connect("192.168.28.91", "root", "123qweasd", "bms_test") # 这里是需要同步的数据库链接
        #db = pymssql.connect('172.16.10.40', 'sa', 'wlyd@123', 'BMS')
        SqlSata.__init__(self, db, "BillingDetail")  # 这里是需要同步的数据库表
        

    # 这里将mongodb的数据转换为其他数据库需要的数据
    def convert(self, data):
        if "CreationTime" not in data:
            print(data)
        rs = {
            "CreationTime": self.get(data, "CreationTime", None),
            "CreatorUserId": self.get(data, "CreatorUserId", 0),
            "LastModificationTime": self.get(data, "LastModificationTime", None),
            "LastModifierUserId": self.get(data, "LastModifierUserId", None),
            "IsDeleted": self.get(data, "IsDeleted", None),
            "TenantId": self.get(data, "TenantId", None),
            "CreationUserName": self.get(data, "CreationUserName", None),
            "LastModifierUserName": self.get(data, "LastModifierUserName", None),
            "DeleteUserName": self.get(data, "DeleteUserName", None),
            "Code": self.get(data, "Code", ""),
            "_id": self.get(data, "_id", ""),
            "Contract_Code": self.get(data, "Contract.Code", None),
            "Contract_Name": self.get(data, "Contract.Name", None),
            "Contract_CustomerCode": self.get(data, "Contract.CustomerCode", None),
            "Contract_CustomerName": self.get(data, "Contract.CustomerName", None),
            "Contract_BelongCompanyId": self.get(data, "Contract.BelongCompanyId", None),
            "Contract_BelongCompanyName": self.get(data, "Contract.BelongCompanyName", None),
            "Contract_Category": self.get(data, "Contract.Category", None),
            "Project_Code": self.get(data, "Project.Code", None),
            "Project_Name": self.get(data, "Project.Name", None),
            "SettlementItem_Code": self.get(data, "SettlementItem.Code", None),
            "SettlementItem_Name": self.get(data, "SettlementItem.Name", None),
            "SettlementItem_ServiceItemCode": self.get(data, "SettlementItem.ServiceItemCode", None),
            "SettlementItem_ServiceItemName": self.get(data, "SettlementItem.ServiceItemName", None),
            "BusinessNumber": self.get(data, "BusinessNumber", None),
            "BusinessType": self.get(data, "BusinessType", None),
            "StandingBookCode": self.get(data, "StandingBookCode", None),
            "Amount": self.get(data, "Amount", None),
            "GenerationDate": self.get(data, "GenerationDate", None),
            "ExpectBillingDate": self.get(data, "ExpectBillingDate", None),
            "RealBillingDate": self.get(data, "RealBillingDate", None),
            "BillCode": self.get(data, "BillCode", None),
            "Status": self.get(data, "Status", None),
            "Type": self.get(data, "Type", None),
            "FeeType": self.get(data, "FeeType", None),
            "ProductCode": self.get(data, "ProductCode", None),
            "ProductName": self.get(data, "ProductName", None),
            "Tax": self.get(data, "Tax", 0)
        }
        for k in rs:
            t = type(rs[k]).__name__
            if t == "datetime":
                rs[k] = rs[k] + datetime.timedelta(hours=8)

        return rs


class Test1(MongoSync):
    def __init__(self):
        # 这里是mongodb数据库链接
        MongoSync.__init__(
            self, "mongodb://root:!(%2FQqi%2B9ka0y@192.168.200.108:27017/?authSource=admin&replicaSet=rs0", Bl_SqlSata())

    def other_query(self, query):
        # 需要增量更新时，填写表名
        # query["ns"] = "bms_prd.BillingDetail"        
        pass



t = Test1()
print(t.get_last_ts())
t.full_run("bms_prd.BillingDetail") # 全量更新更强

# 增量更新

#t.run()
#t.end()
